package com.javasee.rain.stock.service;

import com.javasee.rain.stock.Result;
import com.javasee.rain.stock.mapper.ZhishuMapper;
import com.javasee.rain.stock.model.Zhishu;
import com.nlf.calendar.Holiday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project Name: rain-stock
 * Package Name: com.javasee.rain.stock.service
 * Description :
 *
 * @author : xinyanfei
 * @date : 2025/10/9 14:53
 * Copyright (c) 2025, 京东商城  All Rights Reserved.
 */
@RestController
@RequestMapping("/api/zhishu")
@CrossOrigin(origins = "*") // 允许跨域访问
public class ZhishuService {

    @Autowired
    private ZhishuMapper zhishuMapper;

//    @ResponseBody
//    @RequestMapping("/get")
//    public List<Zhishu> getZhishus() {
//        List<Zhishu> data = zhishuMapper.selectByDate("2025-09-25");
//        System.out.println("usersMapper.findAll() = " + data);
//        return data;
//    }

    @GetMapping("/{dateTime}")
    public ResponseEntity<List<Zhishu>> getZhishuByDate(@PathVariable String dateTime) {
        try {
            System.out.println("dateTime = " + dateTime);
            if (!dateTime.contains("-")) {
                dateTime = dateTime.substring(0, 4) + "-" + dateTime.substring(4, 6) + "-" + dateTime.substring(6);
            }
            List<Zhishu> zhishuList = zhishuMapper.selectByDate(dateTime);
            return ResponseEntity.ok(zhishuList);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @GetMapping("/get")
    public ResponseEntity<Map<String, Object>> getAllZhishu() {
        try {
           List<Zhishu> zhishuList = zhishuMapper.selectByDate("2025-09-25");;

            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", zhishuList);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }
}
